//
//  WZCellViewModel.swift
//  weibo
//
//  Created by mac on 16/3/1.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SDWebImage
class WZCellViewModel: NSObject {
    
    
    var model: WZStatesModel?
    var pictureViewModel: WZPictureModel?
    init(model: WZStatesModel) {
        self.model = model
    }
    
    //            ///  created_at	string	微博创建时间
    //            var created_at: String?
    //            /// text	string	微博信息内容
    //            var text: String?
    //            ///source	string	微博来源
    //            var source: String?
    //            //  screen_name	string	用户昵称
    //            var screen_name: String?
    //            /// profile_image_url	string	用户头像地址（中图），50×50像素
    //            var profile_image_url: String?
    //            /// 会员等级 1-6
    //            var mbrank: Int = 0
    
 
    var levelImage: UIImage?{
       return UIImage(named: "common_icon_membership_level\(model?.user?.mbrank ?? 0)")
        
    }
    
    override init() {
        super.init()
    }
    
  //    MARK:设置微博内容
    var retweetString: String?{
        
        
            return model?.retweeted_status?.text
    }
    
//   MARK: 微博类型
    var veriftyImage: UIImage? {
        switch model?.user?.verified_type ?? -1 {
        case -1: return nil
        case 0: return UIImage(named: "avatar_vip")
        case 2,3,5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
    //MARK:reposts_count	int	转发数
    var reposts_count:String{
        if let reposts_count = model?.reposts_count{
            
            if reposts_count == 0{
                return "转发"
            }
            return "\(reposts_count)"
        }
        return "转发"
    }
    

    //MARK:comments_count	int	评论数
    var commentsCount: String?{
//        
        if let comments_count = model?.comments_count {
            
            if comments_count == 0{
                return "评论"
            }
            return "\(comments_count)"
        }
        return "评论"
        
    }
      //MARK: attitudes_count	int	点赞
    
    
    
    //    MARK:创建方法返回微博来源
    func realSource(sourceString: String)->String?{
        /* Optional("<a href=\"http://app.weibo.com/t/feed/4fuyNj\" rel=\"nofollow\"
        即刻笔记</a -> 即刻笔记 /a
        ")
        */
        // Optional("<a href=\"http://app.weibo.com/t/feed/qsbvs\" rel=\"nofollow\">手机微博触屏版</a>")
        // Optional("<a href=\"http://app.weibo.com/t/feed/1SAfHQ\" rel=\"nofollow\">百度浏览器</a>")
        guard let str = model?.source else{
            return nil
        }
      let realArray = str.componentsSeparatedByString(">")
        print(realArray)
        if realArray.count >= 2{
            let str = realArray[1]
            
            let array = str.componentsSeparatedByString("<")
            if array.count > 1{
                return "来自\(array[0])"
            }else{
                return nil
            }
        }else{
            return nil
        }
        
//        MARK:创建方法设置微博时间
        
        
    }
    func creatAtTime(creatAtTime: String?)->String?{

        guard let creatTime = creatAtTime else{
            return nil
        }
        let formatter = NSDateFormatter()
        formatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        formatter.locale = NSLocale(localeIdentifier: "cn")
        let time = formatter.dateFromString(creatTime)
        
        let calendar = NSCalendar.currentCalendar()
        
        let calendarUnit = NSCalendarUnit(arrayLiteral: .Year,.Month,.Day,.Hour,.Minute)
        
        if let Odate = time{
            
            let comps = calendar.components(calendarUnit, fromDate:Odate , toDate: NSDate(), options: [])
            
            if comps.year == 0 { // 应该是今年
                
                if comps.month == 0 {   // 是本月
                    
                    if comps.day == 0 { // 是今天
                        
                        if comps.hour == 0 {    // 1小时之内
                            
                            if comps.minute == 0 {// 刚刚
                                return "刚刚"
                            } else { //xx分钟前
                                return "\(comps.minute)分钟前"
                            }
                            
                        } else {    //xx小时前
                            return "\(comps.hour)小时前"
                        }
                        
                    } else if comps.day == 1 { //是昨天 -- "昨天 HH:mm"
                        // 时间怎么获取 -- 如果你想要 时间格式字符串的话,
                        // 可以把标准时间 2016-01-09 01:47:03 +0000,
                        // 按照指定时间格式,返回一个字符串
                        
                        formatter.dateFormat = "HH:mm"
                        // 把一个时间转换成 String类型.
                        let str = formatter.stringFromDate(time!)
                        return "昨天 \(str)"
                        
                    }else { // 本月的其他天 "MM-dd HH:mm"
                        
                        formatter.dateFormat = "MM-DD HH:mm"
                        return "\(formatter.stringFromDate(time!))"
                        
                    }
                    
                }else { //不是本月  "MM-dd HH:mm"
                    formatter.dateFormat = "MM-DD HH:mm"
                    return "\(formatter.stringFromDate(time!))"
                }
                
            } else { // 不是今年 "yyyy-MM-dd"
                formatter.dateFormat = "yyyy-MM-dd"
                return "\(formatter.stringFromDate(time!))"
            }
            
        }
        return nil
    }
}
